import WxValidate from '../../assets/plugins/wx-validate/WxValidate'
var bridgeApi = require('../../utils/bridgeApi.js')
const app = getApp();
Page({
  
  data: {
    subm: false,
    structureTypeNames: [],
    structureTypes : [],
    index: 0,
    structureTypeId: '',
    structureTypeName: '',
    structureTypeValue: '',
    organization: '',
    bridgeName: '',
    bridgeWidth: null,
    bridgeColspan: null,
    bridgeLength: null,
    spanExpression: null,
    concrete: null,
    steelStrand: null,
    rebar: null,
    steel: null,
    stayCable: null,
    mainCableBoom: null,
    average: null,
    spanExpression: null
  },
  onLoad: function (options) {
    this.initValidate();
    let structureTypes = wx.getStorageSync("structureTypes");
    if (!structureTypes) {
      bridgeApi.getStructureTypes().then((data) => {
        this.setData({structureTypes: data});
        this.setStructTypes(data);
        wx.setStorageSync("structureTypes", data);
      });
    }else{
      this.setData({ structureTypes: structureTypes });
      this.setStructTypes(structureTypes);
    }
  },
  setStructTypes: function (structureTypes) {
    var structureTypeNames = [];

    for (let item of structureTypes) {
      structureTypeNames.push(item.name);
    }
    this.setData({
      structureTypeNames: structureTypeNames,
      structureTypeId: structureTypes[0].id,
      structureTypeName: structureTypes[0].name,
      structureTypeValue: structureTypes[0].code,
    });
  },
  formSubmit: function (e) {

    if (!this.WxValidate.checkForm(e.detail.value)) {
      if (this.WxValidate.errorList &&
        this.WxValidate.errorList.length) {
        this.showModal(this.WxValidate.errorList[0]);
        return false;
      }
    }

    var token = app.globalData.openId;
    var that = this;

    let bridgeData = {
      across: that.data.bridgeColspan,
      len: that.data.bridgeLength,
      name: that.data.bridgeName,
      totalBeton: that.data.concrete,
      totalCableBoom: that.data.mainCableBoom,
      totalRolledSteel: that.data.steel,
      totalStayCable: that.data.stayCable,
      totalSteelBar: that.data.rebar,
      totalSteelStrand: that.data.steelStrand,
      userId: app.globalData.openId,
      userName: app.globalData.userInfo.nickName,
      width: that.data.bridgeWidth,
      spanExpression: that.data.spanExpression,
      organization: that.data.organization,
      structureTypeId: that.data.structureTypeId,
      structureTypeName: that.data.structureTypeName,
      structureTypeValue: that.data.structureTypeValue,
    };

    // 提交
    wx.request({
      url: app.globalData.siteUrl + '/bridge/save',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "X-Token": token,
      },
      data: bridgeData,
      success: function (res) {
        wx.showToast({
          title: '提交成功！！！！',
          icon: 'success',
          duration: 1000,
        });
        setTimeout(function () {
          wx.reLaunch({
            url: '../show/show',
            success: function (e) {
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page.onLoad();
            }
          });
        }, 500);
      }
    });
  },
  bindCancel: function (e) {
    wx.navigateBack({});
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    });
    var item = this.data.structureTypes[this.data.index];
    this.setData({
      structureTypeId: item.id,
      structureTypeName: item.name,
      structureTypeValue: item.code,
    });
  },
  inputBridgeData: function (e) {
    let dataKey = e.currentTarget.dataset.name;
    this.setData({
      [dataKey]: e.detail.value,
    });

    if (this.data.bridgeLength && this.data.bridgeColspan) {
      let len = this.data.bridgeLength;
      let col = this.data.bridgeColspan;
      let av = len / col;
      this.setData({
        average: av
      });
    }
  },
  // 表单验证规则设置
  initValidate() {
    // 验证字段的规则
    const rules = {
      bridgeName: {
        required: true,
      },
      bridgeLength: {
        required: true,
        number: true,
      },
      bridgeColspan: {
        required: true,
        digits: true,
      },
      bridgeWidth: {
        required: true,
        number: true,
      },
      concrete: {
        required: true,
        number: true,
      },
      steelStrand: {
        required: true,
        number: true,
      },
      rebar: {
        required: true,
        number: true,
      },
      steel: {
        required: true,
        number: true,
      },
      stayCable: {
        required: true,
        number: true,
      },
      mainCableBoom: {
        required: true,
        number: true,
      },
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      bridgeName: {
        required: '请输入桥梁名称',
      },
      bridgeLength: {
        required: '请输入桥梁长度',
        number: '请输入正确的桥梁长度',
      },
      bridgeColspan: {
        required: '请输入跨数n',
        digits: '请输入正确的跨数n',
      },
      bridgeWidth: {
        required: '请输入桥梁宽度',
        number: '请输入正确的桥梁宽度',
      },
      concrete: {
        required: '请输入混凝土量',
        number: '请输入正确的混凝土数量',
      },
      steelStrand: {
        required: '请输入钢绞线',
        number: '请输入正确的钢绞线量',
      },
      rebar: {
        required: '请输入普通钢筋量',
        number: '请输入正确的普通钢筋量',
      },
      steel: {
        required: '请输入钢材量',
        number: '请输入正确的钢材量',
      },
      stayCable: {
        required: '请输入斜拉索',
        number: '请输入正确的斜拉索量',
      },
      mainCableBoom: {
        required: '请输入主缆及吊杆',
        number: '请输入正确的主缆及吊杆量',
      }
    }

    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages)
  },

  // 弹出验证提示
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  }
})